#include <Arduino.h>
#include <U8g2lib.h>
#include "Audio.h"

#include <WiFi.h>
#include <WiFiAP.h>
#include <WiFiClient.h>
#include <WiFiGeneric.h>
#include <WiFiMulti.h>
#include <WiFiScan.h>
#include <WiFiServer.h>
#include <WiFiSTA.h>
#include <WiFiType.h>
#include <WiFiUdp.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif


//Digital I/O used  //Makerfabs Audio V2.0
#define ESP32_I2C_SDA 5
#define ESP32_I2C_SCL 4

#define MAX_DELAY_RDS 40
#define MAX_STATIONS 10

//SSD1306
U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 36, /* data=*/ 35, /* cs=*/ 37, /* dc=*/ 33, /* reset=*/ 34);

//Button
const int Pin_vol_up = 1;
const int Pin_vol_down = 2;
const int Pin_pause = 3;
const int Pin_next = 6;

Audio audio;

const char* ssid     = "iron2222";      //网络名称
const char* password = "18436099009";      //网络密码

struct Music_info
{
    String name;
    int length;
    int runtime;
    int volume;
    int status;
    int mute_volume;
} music_info = {"", 0, 0, 0, 0, 0};


int volume = 21;
int mute_volume = 0;
 
int runtime = 0;
int length = 0;

String stations[] = {
    "0n-80s.radionetz.de:8000/0n-70s.mp3",
    "mediaserv30.live-streams.nl:8000/stream",
    "www.surfmusic.de/m3u/100-5-das-hitradio,4529.m3u",
    "stream.1a-webradio.de/deutsch/mp3-128/vtuner-1a",
    "mp3.ffh.de/radioffh/hqlivestream.aac", //  128k aac
    "www.antenne.de/webradio/antenne.m3u",
    "listen.rusongs.ru/ru-mp3-128",
    "edge.audio.3qsdn.com/senderkw-mp3",
    "macslons-irish-pub-radio.com/media.asx"};
int station_index = 0;
int station_count = sizeof(stations) / sizeof(stations[0]);

void setup() {
  
    //IO mode init
    pinMode(Pin_vol_up, INPUT_PULLUP);
    pinMode(Pin_vol_down, INPUT_PULLUP);
    pinMode(Pin_pause, INPUT_PULLUP);
    pinMode(Pin_next, INPUT_PULLUP);

    //Serial
    Serial.begin(9600);
    
    //OLCD
    u8g2.begin();
    dispwelcome();

    //connect to WiFi
    WiFi.begin(ssid, password);
  Serial.print("Connecting to ");              // 串口监视器输出网络连接信息
  Serial.print(ssid); Serial.println(" ...");  // 告知用户正在尝试WiFi连接
  int i = 0;                                   // 这一段程序语句用于检查WiFi是否连接成功
  while (WiFi.status() != WL_CONNECTED) {      // WiFi.status()函数的返回值是由NodeMCU的WiFi连接状态所决定的。 
    delay(1000);                               // 如果WiFi连接成功则返回值为WL_CONNECTED                       
    Serial.print(i++); Serial.print(' ');      // 此处通过While循环让NodeMCU每隔一秒钟检查一次WiFi.status()函数返回值
  }                                            // 同时NodeMCU将通过串口监视器输出连接时长读秒。
                                               // 这个读秒是通过变量i每隔一秒自加1来实现的。                                           
  Serial.println("");                          // WiFi连接成功后
  Serial.println("Connection established!");   // NodeMCU将通过串口监视器输出"连接成功"信息。
  Serial.print("IP address:    ");             // 同时还将输出NodeMCU的IP地址。这一功能是通过调用
  Serial.println(WiFi.localIP());              // WiFi.localIP()函数来实现的。该函数的返回值即NodeMCU的IP地址。

  //Audio(I2S)
    audio.setPinout(ESP32_I2C_SCL, ESP32_I2C_SDA, I2S_DOUT);
    audio.setVolume(21); // 0...21
 
    open_new_radio(stations[station_index]);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void dispwelcome() {
  u8g2.firstPage();
  u8g2.clearBuffer();         //清除内部缓冲区
  u8g2.setFontDirection(0);   //设置字体方向
  u8g2.setFont(u8g2_font_ncenB14_tr);     //设置字体
  u8g2.setCursor(0, 40);
  u8g2.print("Hello Jarvis!");
  u8g2.sendBuffer();
  delay(1000);
}
